<?php

namespace App\Http\Resources;

use App\Models\ProductTranslation;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Support\Facades\Auth;
class PosProductCollection extends ResourceCollection
{
    public function toArray($request)
    {
        return [
            'data' => $this->collection->map(function ($data) {
                $lang = session('locale');
                $name = $data->name;
                if ($lang != 'en') {
                    $name = ProductTranslation::query()->where('product_id', $data->id)->where('lang', $lang)->pluck('name');
                    $name = isset($name[0]) ? $name[0] : $data->name;
                }
                return [
                    'id' => $data->id,
                    'stock_id' => $data->stock_id,
                    'name' => $name,
                    'thumbnail_image' => ($data->stock_image == null) ? uploaded_asset($data->thumbnail_img) : uploaded_asset($data->stock_image),
                    'price' => home_discounted_base_price_by_stock_id($data->stock_id),
                    'base_price' => home_base_price_by_stock_id($data->stock_id),
                    'qty' => $data->stock_qty,
                    'variant' => $data->variant,
                    'products_original_price' => $data->products_original_price == null ? 0 : $data->products_original_price,
                ];
            })
        ];
    }
    public function toArray_new($request)
    {
        return [
            'data' => $this->collection->map(function ($data) {
                $lang = session('locale');
                $name = $data->name;
                if ($lang != 'en') {
                    $name = ProductTranslation::query()->where('product_id', $data->id)->where('lang', $lang)->pluck('name');
                    $name = isset($name[0]) ? $name[0] : $data->name;
                }
                $shop = Auth::user()->shop;
                $maxProfit = 0.28;
                if(isset($shop->seller_package)){
                    $sellerPackage = $shop->seller_package;
                    if (!$sellerPackage) {
                        return response()->json(['success' => 0, 'message' => translate('Please upgrade your package.')]);
                    }
                    $maxProfit = $sellerPackage->max_profit / 100;
                };
                
                $unit_price = floatval($data->unit_price) +  floatval($data->unit_price* $maxProfit);
                return [
                    'id' => $data->id,
                    'stock_id' => $data->stock_id,
                    'name' => $name,
                    'thumbnail_image' => ($data->stock_image == null) ? uploaded_asset($data->thumbnail_img) : uploaded_asset($data->stock_image),
                    'price' => home_discounted_base_price_by_stock_id($data->stock_id),
                    'base_price' => home_base_price_by_stock_id($data->stock_id),
                    'qty' => $data->stock_qty,
                    'variant' => $data->variant,
                    'unit_price' => number_format($unit_price,2),
                ];
            })
        ];
    }

    public function with($request)
    {
        return [
            'success' => true,
            'status' => 200
        ];
    }
}
